#include "include/Angel.h"
/*
 * main.cpp
 *
 *  Created on: 2014-01-31
 *      Author: sunghanc
 */

GLfloat Translation[2] = {0.0, 0.85};
GLuint locT;

GLfloat angle = 0; //in radian
GLuint locR;

// The rotation  around z axis
GLuint  locTheta;

void drawGrid(){
	// draw background grid
	glBegin(GL_LINES);
	glColor3f(0.20, 0.20, 0.20);

	// draw colums
	for(int i = 0; i < 10; i++){
		glVertex2f(1-(i*0.2), -1);
		glVertex2f(1-(i*0.2), 1);
	}

	// draw raws
	for(int i = 0; i < 20; i++){
		glVertex2f(-1, 1-(i*0.1));
		glVertex2f(1, 1-(i*0.1));
	}
	glEnd();
}

void t_up(){
	vec2 points[] = {
		//   **
		// ******
			vec2(-0.05, 0.15),
			vec2(0.05, 0.15),
			vec2(0.05, 0.05),
			vec2(-0.05, 0.05),
			vec2(-0.15, 0.05),
			vec2(0.15, 0.05),
			vec2(0.15, -0.05),
			vec2(-0.15, -0.05)
	};

	vec4 colors[] = {
		vec4( 0.0, 1.0, 0.0, 0.5 ),  // green
		vec4( 0.0, 1.0, 0.0, 0.5 ),  // green
		vec4( 0.0, 1.0, 0.0, 0.5 ),  // green
		vec4( 0.0, 1.0, 0.0, 0.5 ),  // green
		vec4( 0.0, 1.0, 0.0, 0.5 ),  // green
		vec4( 0.0, 1.0, 0.0, 0.5 ),  // green
		vec4( 0.0, 1.0, 0.0, 0.5 ),  // green
		vec4( 0.0, 1.0, 0.0, 0.5 )  // green
	};

	// Create and initialize a buffer object
	GLuint buffer;
	glGenBuffers( 1, &buffer );
	glBindBuffer( GL_ARRAY_BUFFER, buffer );
	glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors), NULL, GL_STATIC_DRAW );

	glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points );
	glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors );

    // Load shaders and use the resulting shader program
	GLuint program = InitShader( "vshader.glsl", "fshader.glsl" );
	glUseProgram( program );

    // set up vertex arrays
	GLuint vPosition = glGetAttribLocation( program, "vPosition" );
	glEnableVertexAttribArray( vPosition ); // this updates (x, y) position through shader
	glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE,	0, BUFFER_OFFSET(0));

	GLuint vColor = glGetAttribLocation( program, "vColor" );
	glEnableVertexAttribArray( vColor );
	glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0,
		BUFFER_OFFSET(sizeof(points)) );

	// The location of the  shader uniform variable "deltaT"
    locT = glGetUniformLocation( program, "translation" );
    locR = glGetUniformLocation( program, "angle");

}

void init_block(){
	// Specify the vertices for a square
	t_up();
}

void init (){
	init_block();
	// clear the color buffer
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

void display(){
	glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT );

	glShadeModel(GL_FLAT);

	// rotate rect
	glUniform1f(locR, angle);

	// Rendering function.
	glUniform4fv( locT,1, Translation );

	glDrawArrays(GL_QUADS, 0, 8);

	glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y){
	switch(key){
	case 033:
	case 'q':
	case 'Q':
		exit(EXIT_SUCCESS);
		break;
	}
}

void processSpecialKeys(int key, int x, int y){
	switch(key){
		case GLUT_KEY_UP:
			angle -= 90;
			break;
		case GLUT_KEY_DOWN:
			if(Translation[1] <= -0.9){
				Translation[1] = -0.9;
			}
			else{
				Translation[1] -= 0.1;
			}
			break;
		case GLUT_KEY_LEFT:
			if(Translation[0] <= -0.85){
				Translation[0] = -0.85;
			}
			else{
				Translation[0] -= 0.2;
			}
			break;
		case GLUT_KEY_RIGHT:
			if(Translation[0] >= 0.85){
				Translation[0] = 0.85;
			}
			else{
				Translation[0] += 0.2;
			}
			break;
	}

	glutPostRedisplay();
}

void idle(){
	Translation[1] += -0.002;

	if(Translation[1] < -0.85){
		Translation[1] = -0.85;
	}
	glutPostRedisplay();
}

//----------------------------------------------------------------------------

int main (int argc, char **argv){
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(300, 600);
	glutCreateWindow("Simple Tetris!");

	glewInit();
	init();

	glutDisplayFunc(display);
	glutKeyboardFunc(keyboard);
	glutSpecialFunc(processSpecialKeys);
	glutIdleFunc(idle);

	// enters event loop
	glutMainLoop();
	return 0;
}
